﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BookExchange.Business;
using BookExchange.CustomClass.Base;
using BookExchange.ODataService;
using BookExchange.Configuration;

namespace BookExchange
{
    public partial class _Default : BasePage
    {

        #region formatString
        private string _formatDate = "{0:" + AppConfiguration.Instance.DateFormat + "}";
        private string _formatQuantity = "{0:" + AppConfiguration.Instance.IntegerFormat + "}";
        private string _formatPrice1 = "{0:" + AppConfiguration.Instance.CurrencyFormat + "}";
        private string _formatPrice2 = "{0:" + AppConfiguration.Instance.CurrencyFormat + "}";


        public string formatDate
        {
            get { return _formatDate; }

        }
        public string formatQuantity
        {
            get { return _formatQuantity; }
        }
        public string formatPrice1
        {
            get { return _formatPrice1; }
        }
        public string formatPrice2
        {
            get { return _formatPrice2; }
        }
        #endregion




        //private CategoryBusiness categoryBus;
        //private RequestBusiness requestBus;
        //private List<Request> requestList = new List<Request>();
        //private List<Request> postList = new List<Request>();
        //private int activePostQty;
        //private int activeRequestQty;



        private string queryStringRequest = "allrequest=1";
        private string queryStringPost = "allpost=1";
        private RequestBusiness requestBus;
         
        static private int itemPerPage = AppConfiguration.Instance.MaxItemPerPage;
        static IQueryable<ODataService.Request> queryRequest;
        static IQueryable<ODataService.Request> queryPost;
        static int requestCount;
        static int postCount;

        public _Default()
            : base()
        {
            // categoryBus = new CategoryBusiness();
            requestBus = new RequestBusiness();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
             Response.Redirect("SearchAll.aspx?allpost=1");
            //LoadData();

            //BindData();

            //if (IsPostBack)
            //{
            //    return;
            //} 
            //IQueryable<ODataService.Request> query = requestBus.GetQueryDefault();

            //LoadRequest(query);
            //LoadPost(query);
            //get query RequestType

        }

        private void LoadPost(IQueryable<ODataService.Request> query)
        {
            queryPost = requestBus.GetQueryFilterRequestType(query, RequestType.Post);



            //Post
            postCount = requestBus.GetCountActiveRequest(queryPost);
            lblPostQty.Text = postCount.ToString() + string.Empty;

            AspNetPagerPost.RecordCount = postCount;
            AspNetPagerPost.PageSize = itemPerPage;

            if (postCount <= itemPerPage)
                hplRealAllPost.Visible = false;
            else
                hplRealAllPost.NavigateUrl = "~/SearchAll.aspx?" + queryStringPost;

            BindDataPost();

        }


        private void LoadRequest(IQueryable<ODataService.Request> query)
        {
            //Request  
            queryRequest = requestBus.GetQueryFilterRequestType(query, RequestType.Request);
            requestCount = requestBus.GetCountActiveRequest(queryRequest);
            lblRequestQty.Text = requestCount.ToString() + string.Empty;

            AspNetPagerRequest.RecordCount = requestCount;
            AspNetPagerRequest.PageSize = itemPerPage;

            if (requestCount <= itemPerPage)
                hplReadAllRequest.Visible = false;
            else
                hplReadAllRequest.NavigateUrl = "~/SearchAll.aspx?" + queryStringRequest;

            BindDataRequest();

        }

        private void BindDataPost()
        {
            int skipItem = AspNetPagerPost.StartRecordIndex - 1;

            List<Request> postList = requestBus.GetListActiveRequest(queryPost, skipItem, itemPerPage);
            repPost.DataSource = postList;
            repPost.DataBind();
        }

        private void BindDataRequest()
        {
            int skipItem = AspNetPagerRequest.StartRecordIndex - 1;

            List<Request> requestList = requestBus.GetListActiveRequest(queryRequest, skipItem, itemPerPage);
            repRequest.DataSource = requestList;
            repRequest.DataBind();
        }



        protected void AspNetPagerPost_PageChanged(object src, EventArgs e)
        {
            //if current page > 2
            if (AspNetPagerPost.StartRecordIndex > itemPerPage)
            {
                Session["skip"] = AspNetPagerPost.StartRecordIndex - 1;
                Session["RequestCount"] = postCount;
                Session.Timeout = 5;
                Response.Redirect("SearchAll.aspx?" + queryStringPost);
            }
            else
                BindDataPost();
        }


        protected void AspNetPagerRequest_PageChanged(object src, EventArgs e)
        {
            //if current page > 2
            if (AspNetPagerRequest.StartRecordIndex > itemPerPage)
            {
                Session["skip"] = AspNetPagerRequest.StartRecordIndex - 1;
                Session["RequestCount"] = requestCount;
                Session.Timeout = 5;
                Response.Redirect("SearchAll.aspx?" + queryStringRequest);
            }
            else
                BindDataRequest();
        }


        /*     public void LoadData()
              {
                  requestList = requestBus.GetTopRequest(RequestType.Request, AppConfiguration.Instance.MaxItemPerPage);
            
                  postList = requestBus.GetTopRequest(RequestType.Post, AppConfiguration.Instance.MaxItemPerPage);

                  activePostQty = requestBus.CountActiveRequest(RequestType.Post);

                  activeRequestQty = requestBus.CountActiveRequest(RequestType.Request);                
              }

              public void BindData()
              {
                  if (!IsPostBack)
                  {
                      lblActivePostQty.Text = activePostQty + string.Empty;
                      repPost.DataSource = postList;
                      repPost.DataBind();

                      lblActiveRequestQty.Text = activeRequestQty + string.Empty;
                      repRequest.DataSource = requestList;
                      repRequest.DataBind();
                  }
              }
         */
    }
}